<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="名称"
                    ></el-input>
                </div>
            </div>
            <div v-if="queryData.examType !== 'gjtk'" class="search-item">
                <label>行业：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.industryId"
                        v-loading="industryFlag"
                        filterable
                        clearable
                        placeholder="请选择行业"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in industryData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>考试：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.examId"
                        v-loading="examFlag"
                        filterable
                        clearable
                        placeholder="请选择考试"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in examData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>

        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="industryName"
                    label="行业名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="examName"
                    label="考试名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="agentPrice"
                    label="代理价格￥"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        {{ priceChange(scope.row.agentPrice) }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="days"
                    label="有效天数"
                    min-width="100"
                ></el-table-column>
                <!--                <el-table-column-->
                <!--                    prop="endDate"-->
                <!--                    label="有效日期"-->
                <!--                    min-width="100"-->
                <!--                ></el-table-column>-->
                <!--                <el-table-column-->
                <!--                    prop="tagNameList"-->
                <!--                    label="标签名称"-->
                <!--                    min-width="100"-->
                <!--                >-->
                <!--                    <template slot-scope="scope">-->
                <!--                        <el-tag v-for="(item) in scope.row.tagNameList" :key="item" >{{item }}</el-tag>-->
                <!--                    </template>-->
                <!--                </el-table-column>-->

                <el-table-column
                    label="操作"
                    width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            :loading="scope.row.opLoading"
                            size="mini"
                            type="text"
                            icon="el-icon-folder-opened"
                            @click="studyDetail(scope.row)"
                        >sku开课
                        </el-button>
                        <!--                        <el-button-->
                        <!--                            v-if="scope.row.enable"-->
                        <!--                            :loading="scope.row.opLoading"-->
                        <!--                            type="text"-->
                        <!--                            size="mini"-->
                        <!--                            class="warning"-->
                        <!--                            icon="el-icon-lock"-->
                        <!--                            @click="openCode(scope.row)"-->
                        <!--                        >提码-->
                        <!--                        </el-button>-->
                        <el-button
                            v-if="scope.row.enable"
                            :loading="scope.row.opLoading"
                            type="text"
                            size="mini"
                            class="warning"
                            icon="el-icon-lock"
                            @click="openCodeByPackage(scope.row)"
                        >套餐提码
                        </el-button>

                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :visible.sync="showAnaly"
            width="80%"
        >
            <sku-component v-if="showAnaly" ref="analyRef" :package-id="packageId" :industry-id="industryId"
                           :exam-id="examId"></sku-component>
        </el-dialog>
        <el-dialog
            title="提码"
            :modal="false"
            :visible.sync="codeFormVisible"
            :before-close="codeDialogClose"
            custom-class="two-dialog"
        >
            <el-form
                ref="codeForm"
                :model="codeForm"
                label-width="8em"
                class="table-edit-from "
            >

                <el-form-item label="代理商折扣:" prop="user.discount">
                    <span>{{ codeForm.user.discount }}</span>
                </el-form-item>
                <el-form-item label="套餐名称:" prop="row.courseName">
                    <span>{{ codeForm.row.name }}</span>
                </el-form-item>
                <el-form-item label="套餐价格:" prop="row.agentPrice">
                    <span
                    >{{ priceChange(codeForm.row.agentPrice) }}</span>
                </el-form-item>
                <!--                <el-form-item label="提码数量:" prop="userCount">-->
                <!--                    <span >{{codeForm.userCount}}</span>-->
                <!--                </el-form-item>-->
                <el-form-item label="支付金额:" prop="money">
                    <span disabled>
                        {{ priceChange(codeForm.money) }}
                    </span>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="codeDialogClose"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="
                        myCodeDataSave(
                            '/order/or/order/agent/open-code',
                            r => coverDataForm(r)
                        )
                    "
                >保 存</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {execCall, TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import {get, post} from "@/api/request";
import SkuComponent from "@/views/models/agentManage/skuComponent";
import {getCurrentUserAgent} from "@/api/user";

export default {
    name: "PackageProduct",
    components: {SkuComponent},
    mixins: [TablePage],
    data() {
        return {
            codeForm: {
                row: {},
                agent: {},
                user: {},
                userCount: 0,
            },
            initCodeForm: {
                row: {},
                agent: {},
                user: {},
                userCount: 0,
            },
            showAnaly: false,
            packageId: null,
            examType: null,
            industryData: [],
            examData: [],
            industryFlag: false,
            examFlag: false,
            queryData: {
                examType: null,
                base: true,
                name: "",
                industryId: null,
                examId: null,
                enable: true
            },

            initAddForm: {
                id: null,
                name: null,
                price: null,
                agentPrice: null,
                sort: 100,
                industryId: null,
                examId: null,
                categoryId: null,
                columnIds: [],
                days: null,
                endDate: null,
                examType: "zhiye"
            },
        };
    },
    computed: {
        ...mapState({})
    },
    watch: {

        "queryData.industryId"(industryId) {
            this.queryData.examId = null;
            if (industryId) {
                this.loadExam(industryId);
            } else {
                this.examData = [];
            }
        },
    },
    created() {
        this.loadIndustry();
    },
    mounted() {
    },
    methods: {
        studyDetail(data) {
            this.showAnaly = true;
            this.packageId = data.id;
            this.industryId = data.industryId
            this.examId = data.examId
        },
        search(queryData = {}) {
            this.loadTable("/exam/package/page");
        },
        openCode(row) {
            this.codeForm.row = row;
            this.codeForm.userCount = 1;
            getCurrentUserAgent().then(res => {
                this.codeForm.user = res;
                this.codeForm.addAction = true;
                this.codeForm.money = (
                    this.codeForm.userCount *
                    this.codeForm.row.agentPrice *
                    this.codeForm.user.discount
                ).toFixed(2);
                this.codeFormVisible = true;
            });
        },
        codeDialogClose() {
            const w = () => {
                this.codeFormVisible = false;
                this.codeForm = {...this.initCodeForm};
            };
            this.$confirm("数据还未保存，确定关闭吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(w);
        },
        myCodeDataSave(
            url,
            cover = r => {
                const copy = {...r};
                if (r.parent && typeof r.parent === "object") {
                    copy.parent = null;
                }
                if (r.children && typeof r.children === "object") {
                    copy.children = null;
                }
                return copy;
            }
        ) {
            return new Promise((r, j) => {
                this.$refs.codeForm.validate(valid => {
                    if (valid) {
                        r();
                    } else {
                        j();
                    }
                });
            })
                .then(() => {
                    this.submitFlag = true;
                    const promise = post(url, cover(this.codeForm));
                    return promise
                        .then(async res => {
                            this.codeForm = this.initCodeForm;
                            this.codeFormVisible = false;
                            this.$message({
                                message: "提码成功",
                                type: "success"
                            });
                            this.search();
                            return res;
                        })
                        .finally(() => {
                            this.submitFlag = false;
                        });
                })
                .catch(e => {
                    console.error(e);
                });
        },
        coverDataForm(r) {
            var tmp = {
                packageId: r.row.id,
            };
            return tmp;
        },
        loadIndustry() {
            this.industryFlag = true;
            get("/exam/industry/agent/list", {size: 1000})
                .then(res => {
                    this.industryData = res;
                })
                .finally(() => {
                    this.industryFlag = false;
                });
        },
        loadExam(industryId, name) {
            this.examFlag = true;
            get("/exam/exam/exam-by-agent", {
                industryId,
                name,
                size: 1000
            })
                .then(res => {
                    this.examData = res;
                })
                .finally(() => {
                    this.examFlag = false;
                });
        },
        priceChange(number) {
            const bs = number / 100;
            return bs.toFixed(2) + "元";
        },
        openCodeByPackage(packages){
            this.$prompt("请输入提码数量(一次最多100个)", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                inputType: "number"

            }).then(({ value }) => {
                if(value > 100){
                    this.$message({
                        type: "error",
                        message: "一次最多100个"
                    });
                    return;
                }
                post("/order/or/order/agent/open-sku-code-package", {packageId:packages.id, count: value}).then(res => {
                    this.$message({
                        type: "success",
                        message: "提取成功，请在提码记录去查看"
                    });
                })
            })
        },

    }
};
</script>

<style lang="scss" scoped>
::v-deep .el-transfer-panel {
    width: 300px;
}
</style>
